package com.zhou.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.entity.Account;
import com.zhou.entity.Logistics;
import com.zhou.mapper.AdminMapper;
import com.zhou.mapper.DriverMapper;
import com.zhou.mapper.LogisticsMapper;
import com.zhou.service.LogisticsService;
import com.zhou.utis.threadLocal.ThreadLocalUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class LogisticsServiceImpl implements LogisticsService {
    @Resource
    private LogisticsMapper logisticsMapper;
    @Resource
    private DriverMapper driverMapper;
    @Resource
    private AdminMapper adminMapper;
    @Override
    public String add() {
        String id = ThreadLocalUtil.getId();
        if (StringUtils.isEmpty(id)){
            throw new RuntimeException("没有获取到用户id，请重试");
        }
        Account account = adminMapper.selectById(id);
        if (StringUtils.isEmpty(account)){
            throw new RuntimeException("没有查到该条数据，请重试");
        }
        Logistics logistics=new Logistics();
        logistics.setLogisticsId(UUID.fastUUID().toString());
        logistics.setUserInfoId(id);
        int insert = logisticsMapper.insert(logistics);
        if (insert==0){
            throw new RuntimeException("新增数据没有成功");
        }
        return logistics.getLogisticsId();
    }

    @Override
    public List<Logistics> getList(Integer page, Integer pageSize) {
        Page<Logistics> logisticsPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Logistics> wrapper = new LambdaQueryWrapper();
        Page<Logistics> logistics = logisticsMapper.selectPage(logisticsPage, wrapper);
        List<Logistics> records = logistics.getRecords();
        return records;
    }
}
